﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using eShop.Model;
using eShop.DataAccess.Converter;
using eShop.DataAccess;
using eShop.Model.MetaData;

namespace eShop.Business
{
    public class ShippingRateManager
    {
        public static void CreateShippingRate(ShippingRate shippingRate)
        {
            DataAccess.Entity.ShippingRate eShippingRate = ShippingRateConverter.ConvertToEntity(shippingRate);
            if (ShippingRateDAL.Insert(eShippingRate))
            {
                shippingRate.Id = eShippingRate.Id;
            }
        }

        public static void UpdateShippingRate(ShippingRate shippingRate)
        {
            ShippingRateDAL.Update(ShippingRateConverter.ConvertToEntity(shippingRate));
        }

        public static ShippingRate GetShippingRateById(int shippingRateId)
        {
            List<ShippingRate> shippingRates = ShippingRateConverter.ConvertToModelObject(ShippingRateDAL.GetByKeys(shippingRateId, null));
            if (shippingRates.Count == 1)
                return (shippingRates[0]);
            return null;
        }

        public static float GetShippingRateByCountryStateCityWeight(string cityName, string stateName, string countryName, float weight)
        {
            ShippingRate shippingRate = ShippingRateConverter.ConvertToModelObject(ShippingRateDAL.GetByCountryStateCityWeight(cityName, stateName, countryName, weight));
            if (shippingRate == null)
                return (float)SiteSettings.GroundShippingAmount;
            return shippingRate.Rate;
        }

        public static List<ShippingRate> GetShippingRatesPaged(string sort, bool isAsc, int start, int limit)
        {
            List<City> cities = MetaDataManager.GetAllCitiesWithStateAndCountry();
            List<ShippingRate> shippingRates = ShippingRateConverter.ConvertToModelObject(ShippingRateDAL.GetAll(true,start,limit,sort,isAsc));
            ShippingRateConverter.AttachAssociation(shippingRates, cities);
            return shippingRates;
        }

        public static int GetShippingRatesPagedCount()
        {
            throw new NotImplementedException();
        }
    }
}
